package de.velvettear.squirrel.gui;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.kernel.NFCTag;
import de.velvettear.squirrel.kernel.Statics;
import de.velvettear.squirrel.util.LEDHelper;
import de.velvettear.squirrel.util.NFCHelper;
import de.velvettear.squirrel.util.NFCHelper.NFCIntentListener;
import de.velvettear.squirrel.util.NotificationHelper;

public class ScanFrame extends Activity implements NFCIntentListener {

	private static NFCHelper nfcHelper;	
	private static boolean returnTagID = false;
	private static boolean isScanning = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.activity_scanframe);	
	    
	    if (getIntent().hasExtra(Statics.NFC_RETURN_MODE)) {
	    	returnTagID = getIntent().getExtras().getBoolean(Statics.NFC_RETURN_MODE);
	    } else {
	    	returnTagID = false;
	    }
	}
	
	@Override
	public void onResume() {
	    super.onResume();   
	    
	    if (!isScanning) {
	    	startScanning();
	    }
	}

	@Override
	public void onPause() {
		super.onPause();
		
		if (isScanning) {
			stopScanning();
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		stopScanning();
		NotificationHelper.setNotification(this, Statics.NOTIFICATION_ID_NFC);	
	}
		
	private void reactToTag(Intent tagIntent) {
		Log.i("reactToTag()", "BEGIN");
		
		try {
			stopScanning();
			if (tagIntent.hasExtra(NfcAdapter.EXTRA_TAG)) {			
				Tag nfcTag = tagIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);	
				
				String tagID = null;
				
				switch (NFCHelper.getNFCTech(nfcTag.getTechList())) {			
				case 1:
					tagID = NFCHelper.getUID_ISO_15693(nfcTag.getId());	
					break;
				default:
					tagID = NFCHelper.getHexString(nfcTag.getId());	
					break;				
				}
		
				if (returnTagID) {
					if (tagID != null) {						
						if (getCallingActivity() != null) {
							String name = getCallingActivity().toString();
						}
						
						Intent returnIntent = new Intent();
						returnIntent.putExtra(Statics.NFC_RETURN_MODE, tagID);
						setResult(RESULT_OK, returnIntent);
						finish();
					} else {
						setResult(RESULT_CANCELED);
						finish();
					}					
				} else {
					if (tagID != null) {
						NFCTag tag = new NFCTag();
						tag.getTagByTagID(tagID);
						if (tag.isValid()) {
							NFCHelper.executeTagAction(this, tag);
							finish();
						}
					}
				}
			}					
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("reactToTag()", e.getMessage());
			}
		}

		Log.i("reactToTag()", "END");
	}
	
	private void startScanning() {
		isScanning = true;
		LEDHelper.startFlash(this, R.color.blue, 500);
		if (nfcHelper == null) {
			nfcHelper = new NFCHelper(this);
			nfcHelper.setNFCIntentListener(this);
			nfcHelper.enableForeground();
		}	
	}
	
	private void stopScanning() {
		isScanning = false;
		LEDHelper.stopFlash(this);
		if (nfcHelper != null) {
			nfcHelper.disableForeground();
			nfcHelper = null;
		}
	}
		
	@Override
	public void nfcIntentDetected(Intent tagIntent, String action) {
		reactToTag(tagIntent);		
	}
	
	@Override
	protected void onNewIntent(Intent tagIntent) {
		reactToTag(tagIntent);		
	}
	
}
